<template>
  <div class="message-renderer" :class="{ 'ai-message': isAi, 'user-message': !isAi }">
    <div v-html="renderedContent" class="rendered-content"></div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { marked } from 'marked'
import katex from 'katex'
import hljs from 'highlight.js'

const props = defineProps({
  content: {
    type: String,
    required: true
  },
  isAi: {
    type: Boolean,
    default: false
  }
})

// 配置marked
marked.setOptions({
  highlight: function(code, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(code, { language: lang }).value
      } catch (err) {}
    }
    return hljs.highlightAuto(code).value
  },
  breaks: true,
  gfm: true
})

// 渲染内容
const renderedContent = computed(() => {
  if (!props.content) return ''
  
  let content = props.content
  
  // 如果是用户消息，直接返回纯文本（避免XSS）
  if (!props.isAi) {
    return `<div class="user-text">${escapeHtml(content)}</div>`
  }
  
  // 处理数学公式
  content = renderMathFormulas(content)
  
  // 渲染Markdown
  const htmlContent = marked(content)
  
  return htmlContent
})

// 渲染数学公式
function renderMathFormulas(text) {
  // 处理行内数学公式 $...$
  text = text.replace(/\$([^$]+)\$/g, (match, formula) => {
    try {
      return katex.renderToString(formula, { displayMode: false })
    } catch (e) {
      return match // 如果渲染失败，返回原文
    }
  })
  
  // 处理块级数学公式 $$...$$
  text = text.replace(/\$\$([^$]+)\$\$/g, (match, formula) => {
    try {
      return katex.renderToString(formula, { displayMode: true })
    } catch (e) {
      return match // 如果渲染失败，返回原文
    }
  })
  
  // 处理LaTeX格式的数学公式 \(...\) 和 \[...\]
  text = text.replace(/\\\\?\(([^)]+)\\\\?\)/g, (match, formula) => {
    try {
      return katex.renderToString(formula, { displayMode: false })
    } catch (e) {
      return match
    }
  })
  
  text = text.replace(/\\\\?\[([^\]]+)\\\\?\]/g, (match, formula) => {
    try {
      return katex.renderToString(formula, { displayMode: true })
    } catch (e) {
      return match
    }
  })
  
  return text
}

// HTML转义函数
function escapeHtml(text) {
  const div = document.createElement('div')
  div.textContent = text
  return div.innerHTML
}
</script>

<style scoped>
.message-renderer {
  font-size: 14px;
  line-height: 1.6;
}

.ai-message {
  padding: 12px;
  background: linear-gradient(to right, #f8f3ff 0%, #c0c3f6 100%);
  border-radius: 15px;
}

.user-message {
  padding: 12px;
  background: linear-gradient(to right, #e3f2fd 0%, #90caf9 100%);
  border-radius: 15px;
}

.user-text {
  color: #333;
  white-space: pre-wrap;
}

/* Markdown样式 */
.rendered-content :deep(h1),
.rendered-content :deep(h2),
.rendered-content :deep(h3),
.rendered-content :deep(h4),
.rendered-content :deep(h5),
.rendered-content :deep(h6) {
  margin: 16px 0 8px 0;
  color: #2c3e50;
  font-weight: 600;
}

.rendered-content :deep(h1) { font-size: 1.5em; }
.rendered-content :deep(h2) { font-size: 1.3em; }
.rendered-content :deep(h3) { font-size: 1.2em; }

.rendered-content :deep(p) {
  margin: 8px 0;
  color: #333;
}

.rendered-content :deep(ul),
.rendered-content :deep(ol) {
  margin: 8px 0;
  padding-left: 20px;
}

.rendered-content :deep(li) {
  margin: 4px 0;
  color: #333;
}

.rendered-content :deep(blockquote) {
  margin: 12px 0;
  padding: 8px 16px;
  border-left: 4px solid #409eff;
  background: rgba(64, 158, 255, 0.1);
  border-radius: 4px;
}

.rendered-content :deep(code) {
  background: rgba(27, 31, 35, 0.05);
  padding: 2px 4px;
  border-radius: 3px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.9em;
  color: #d73a49;
}

.rendered-content :deep(pre) {
  background: #f6f8fa;
  padding: 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 12px 0;
  border: 1px solid #e1e4e8;
}

.rendered-content :deep(pre code) {
  background: none;
  padding: 0;
  color: #333;
  font-size: 0.9em;
}

.rendered-content :deep(table) {
  border-collapse: collapse;
  width: 100%;
  margin: 12px 0;
  border: 1px solid #e1e4e8;
  border-radius: 6px;
  overflow: hidden;
}

.rendered-content :deep(th),
.rendered-content :deep(td) {
  border: 1px solid #e1e4e8;
  padding: 8px 12px;
  text-align: left;
}

.rendered-content :deep(th) {
  background: #f6f8fa;
  font-weight: 600;
}

.rendered-content :deep(tr:nth-child(even)) {
  background: #f9f9f9;
}

/* 数学公式样式 */
.rendered-content :deep(.katex) {
  font-size: 1.1em;
}

.rendered-content :deep(.katex-display) {
  margin: 16px 0;
  text-align: center;
}

/* 代码高亮样式 */
.rendered-content :deep(.hljs) {
  background: #f6f8fa !important;
  color: #333 !important;
}

.rendered-content :deep(.hljs-keyword) { color: #d73a49; }
.rendered-content :deep(.hljs-string) { color: #032f62; }
.rendered-content :deep(.hljs-comment) { color: #6a737d; }
.rendered-content :deep(.hljs-number) { color: #005cc5; }
.rendered-content :deep(.hljs-function) { color: #6f42c1; }

/* 链接样式 */
.rendered-content :deep(a) {
  color: #409eff;
  text-decoration: none;
}

.rendered-content :deep(a:hover) {
  text-decoration: underline;
}

/* 强调样式 */
.rendered-content :deep(strong) {
  font-weight: 600;
  color: #2c3e50;
}

.rendered-content :deep(em) {
  font-style: italic;
  color: #555;
}

/* 分隔线 */
.rendered-content :deep(hr) {
  border: none;
  border-top: 2px solid #e1e4e8;
  margin: 20px 0;
}
</style>